package me.lc.linklink.layer;

import me.lc.linklink.R;
import me.lc.linklink.core.GameSystem;
import me.lc.linklink.scene.MainScene;
import me.lc.linklink.util.ADHelper;
import me.lc.linklink.util.GameUtil;

import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.actions.CallFunc;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYColor4B;
import com.wiyun.engine.types.WYPoint;

public class LoadingLayer extends ColorLayer {
	public LoadingLayer() {
		super(WYColor4B.make(255, 255, 255, 255));
		// 显示loading动画
		Animation animation = new Animation(0, 0.3f, R.drawable.loading_0,
				R.drawable.loading_1, R.drawable.loading_2,
				R.drawable.loading_3, R.drawable.loading_4,
				R.drawable.loading_5, R.drawable.loading_6);
		animation.autoRelease();
		Animate animate = Animate.make(animation);
		animate.autoRelease();

		Sprite sprite = Sprite.make(R.drawable.loading_0);
		addChild(sprite);
		sprite.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_ActuallyHeight / 2 - 30);
		sprite.autoRelease();
		
		Sprite logo = Sprite.make(R.drawable.logo77);
		logo.autoRelease();
		logo.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_ActuallyHeight / 2 + 20);
		addChild(logo);

		sprite.runAction((Action) RepeatForever.make(animate).autoRelease());
		sprite.runAction((CallFunc) CallFunc.make(this, "loadResource")
				.autoRelease());

		 Sprite wilogo = Sprite.make(Texture2D.makeJPG(R.drawable.wiyun_logo));
		 addChild(wilogo);
		 wilogo.setAnchorPercent(1, 0);
		 wilogo.setPosition(WYPoint.make(GameSystem.Screen_Width, 0));
		 wilogo.autoRelease();
	}

	/**
	 * 加载系统资源
	 */
	public void loadResource() {
		new Thread() {
			public void run() {
				GameSystem.loadAllResource();
				ADHelper.initAD();
				gotoGameMenu();
			}
		}.start();
	}

	/**
	 * 切换到游戏菜单
	 */
	public void gotoGameMenu() {
		GameUtil.switchSceneNormal(MainScene.make());
	}
}
